package fr.uniscol;

import fr.uniscol.model.Classe;
import fr.uniscol.model.Eleve;
import fr.uniscol.model.IUniscolUser;
import fr.uniscol.mvc.SessionExpireeException;
import fr.uniscol.service.IEleveService;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EleveController extends UniscolMainController {

	@Autowired
	private IEleveService eleveService;
	
    @ModelAttribute("menuContextuel")
    public String getMenuContextuel() throws Exception{
        return  "menus/menu_eleves";
    }
    
	@ModelAttribute("eleves") 
	public List<Eleve> getAllEleves(HttpSession session){ 
		
		return eleveService.getAllElevesOfEtablissement(getUtilisateur(session).getBaseSelectionnee());
	}
	
	@RequestMapping(UniscolConstants.LISTER_ELEVES)
	public String list(){
		return "eleves";
	}
    
    // todo Test
    @RequestMapping("/eleves.list.structure")
    @ResponseBody
    public List<Eleve> listElevesOfStructure(@RequestParam("structureid") long structureid){
        
        Classe structure = new Classe();
        structure.setId(structureid);
        
        List<Eleve> eleves = eleveService.getAllElevesOfClasse(structure);
        
        return eleves;
    }
	
    @RequestMapping(UniscolConstants.AJOUTER_ELEVE)
    public ModelAndView addEleve(){
        
        Eleve eleve = new Eleve();
        
        return new ModelAndView("eleve", "eleve", eleve);
    }
    
	@RequestMapping(UniscolConstants.EDITER_ELEVE)
	public ModelAndView editEleve(@RequestParam("eleveId") int eleveId, HttpSession session){
		
        Eleve eleve = eleveService.getEleveById(eleveId);

        eleve.setScolarites(eleveService.getScolaritesOfEleve(eleve));

        return new ModelAndView("eleve", "eleve", eleve);
	}
    
    @RequestMapping(value = UniscolConstants.ENREGISTRER_ELEVE, method = RequestMethod.POST)
    public String saveUser(@ModelAttribute("eleveFormModel") Eleve eleveModel, HttpSession session, BindingResult results){
        
        eleveModel.setBase(getUtilisateur(session).getBaseSelectionnee());
        
        log.debug("ELEVE.NOM=" + eleveModel.getNom());
        log.debug("ELEVE.PRENOM=" + eleveModel.getPrenom()); 
        log.debug("ELEVE.INE=" + eleveModel.getIne());
        
        if(eleveService.saveEleve(eleveModel)){
            alertInfo("eleve.saved.ok", "Nouvel élève crée avec succès", session);
        }else{
            alertError("eleve.saved.nok", "L'élève n'a pas pu être crée", session);
        }
        
        return "redirect:eleves";
    }
}
